﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BE;
using BL;
using System.Data;

using Mapeo;
using System.Collections;
using AjaxControlToolkit;
using NHibernate;

using MySql.Data;
using MySql.Data.MySqlClient;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using System.Diagnostics;
using System.Drawing;
using System.Net;
using System.IO;
//using CrystalDecisions.Windows;

namespace UI.Tesoreria
{
    public partial class FrmSalidaCajaLista : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                CargarListado();
            }
        }


        public void CargarListado()
        {
            LabelErrorListado.Text = "";
            try
            {
                SalidaCaja_BL salidabl = new SalidaCaja_BL();

                String numeracion = txtNumeracionBusqueda.Text;
                String cliente = txtClienteBusqueda.Text;

                GridListado.DataSource = salidabl.listado(numeracion, cliente);
                GridListado.DataBind();

            }
            catch (Exception ex)
            {
                LabelErrorListado.Text = ex.Message.ToString();
            }
        }

        protected void BtnBuscar_Click(object sender, EventArgs e)
        {

        }

        protected void btnNuevo_Click1(object sender, EventArgs e)
        {
            Session["SalidaCaja"] = null;
            Response.Redirect("FrmSalidaCaja.aspx");
        }

        protected void GridListado_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            LabelErrorListado.Text = "";
            try
            {
                if (e.CommandName.Equals("Reporte"))
                {
                    LinkButton lb = (LinkButton)e.CommandSource;
                    GridViewRow gvr = (GridViewRow)lb.NamingContainer;
                    int id = (int)GridListado.DataKeys[gvr.RowIndex].Value;
                    //string nombre = GridListado.SelectedRow.Cells[5].Text;
                    string nombre = (string)this.GridListado.DataKeys[gvr.RowIndex]["NombreSN"];
                    //GenerarReporteSalidaCajaLista(id.ToString());
                    
                    //TO-TEST180914
                    Session["id"] = id;
                    Session["nombre"] = nombre;
                    Response.Redirect("frmReporteSalidaCajaLista.aspx"); 
                }              
            }
            catch (Exception ex)
            {
                CargarListado();
                LabelErrorListado.Text = ex.Message;
            }
        }

        private void GenerarReporteSalidaCajaLista(String idSalidaCaja)
        {
            LabelErrorListado.Text = "";
            try
            {
                ReporteSalidaCaja reporte = new ReporteSalidaCaja();                
                SalidaCaja_BL rcv = new SalidaCaja_BL();                
                DataTable data = rcv.Listado_data_Salidacaja_xid(idSalidaCaja);

                reporte.SetDataSource(data);
                System.IO.MemoryStream rptStream = new System.IO.MemoryStream();
                rptStream = (System.IO.MemoryStream)reporte.ExportToStream(ExportFormatType.PortableDocFormat);
                Response.Clear();
                Response.Buffer = true;
                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", "attachment;filename=" + "REPORTE PE.pdf");

                Response.BinaryWrite(rptStream.ToArray());
                reporte.Close();
                Response.End();

            }
            catch (Exception ex)
            {
                LabelErrorListado.Text = ex.Message;
            }
        }

        protected void GridListado_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}